/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 8/30/13
 * Time: 8:23 PM
 * To change this template use File | Settings | File Templates.
 */
package com.societyGames.codeAlong.state.codeExercise.view.testCase
{
import com.societyGames.codeAlong.state.codeExercise.model.testCase.TestCase;
import com.societyGames.codeAlong.state.codeExercise.model.testCase.TestCaseController;

import flash.events.Event;

public class TestCaseControl extends TestCaseButtonsContainer
{
  private var _testCaseController:TestCaseController;
  private var _testCaseButtons:Vector.<TestCaseButton> = new Vector.<TestCaseButton>();

  public function setTestCaseController(testCaseController:TestCaseController):void
  {
    clearTestCaseController();
    _testCaseController = testCaseController;
    if (_testCaseController != null)
    {
      _testCaseController.selectedCaseChangedEventObject.addEventListener(selectedCaseChangedHandler);
      createTestCaseButtons();
      selectedCaseChangedHandler(null);
    }
  }

  private function clearTestCaseController():void
  {
    if (_testCaseController != null)
    {
      _testCaseController.selectedCaseChangedEventObject.removeEventListener(selectedCaseChangedHandler);
      destroyTestCaseButtons();
    }
  }

  private function createTestCaseButtons():void
  {
    destroyTestCaseButtons();
    for each (var testCase:TestCase in _testCaseController.testCases)
    {
      var testCaseButton:TestCaseButton = new TestCaseButton(testCase);
      testCaseButton.buttonClickEventObject.addEventListener(testCaseButtonClickHandler);
      _testCaseButtons.push(testCaseButton);
      addItem(testCaseButton);
    }
  }

  private function destroyTestCaseButtons():void
  {
    for each(var testCaseButton:TestCaseButton in _testCaseButtons)
    {
      testCaseButton.buttonClickEventObject.removeEventListener(testCaseButtonClickHandler);
    }
    _testCaseButtons.length = 0;
    clearItems();
  }

  private function selectedCaseChangedHandler(event:Event):void
  {
    for each (var testCaseButton:TestCaseButton in _testCaseButtons)
    {
      testCaseButton.enabled = true;
    }
    _testCaseButtons[_testCaseController.selectedTestCase.index].enabled = false;
  }

  private function testCaseButtonClickHandler(event:Event):void
  {
    if (_testCaseController.canSelectCase)
    {
      var testCaseIndex:int = TestCaseButton(event.target).testCaseIndex;
      _testCaseController.selectCase(testCaseIndex);
    }
  }

  public function bounceAllButtons():void
  {
    var percentDelayPerButton:Number = 1 / _testCaseButtons.length;
    var currentPercentDelay:Number = 0;
    for (var index:int = _testCaseButtons.length - 1; index >= 0; index--)
    {
      var testCaseButton:TestCaseButton = this._testCaseButtons[index];
      testCaseButton.startBounce(currentPercentDelay);
      currentPercentDelay += percentDelayPerButton;
    }
  }
}
}
